﻿/*
	Copyright (c) 2013 Andrew Meyer

	Permission is hereby granted, free of charge, to any person obtaining a copy of this software
	and associated documentation files (the "Software"), to deal in the Software without
	restriction, including without limitation the right to use, copy, modify, merge, publish,
	distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
	Software is furnished to do so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in all copies or
	substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
	BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
	NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
	DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

using System;

namespace Ascend
{
	/// <summary>
	/// An interval of frames over which an Ascend.AnimatedSceneNode can be animated.
	/// </summary>
	public struct FrameInterval : IEquatable<FrameInterval>
	{
		#region Lifecycle
		/// <summary>
		/// Initializes a new instance of the Ascend.FrameInterval structure with given starting and
		/// ending frames.
		/// </summary>
		/// <param name="start">
		/// Starting frame number.
		/// </param>
		/// <param name="end">
		/// Ending frame number.
		/// </param>
		/// <exception cref="System.ArgumentOutOfRangeException">
		/// Thrown if <paramref name="start"/> is greater than <paramref name="end"/>.
		/// </exception>
		public FrameInterval(int start, int end)
		{
			if (start > end)
			{
				throw new ArgumentOutOfRangeException("start", start,
					"Start frame number cannot be greater than end frame number.");
			}
			
			_start = start;
			_end = end;
		}
		#endregion

		#region Public Properties
		private int _start;
		/// <summary>
		/// Gets the starting frame number.
		/// </summary>
		public int Start
		{
			get
			{
				return _start;
			}
		}

		private int _end;
		/// <summary>
		/// Gets the ending frame number.
		/// </summary>
		public int End
		{
			get
			{
				return _end;
			}
		}

		/// <summary>
		/// Gets the duration of this Ascend.FrameInterval in frames.
		/// </summary>
		public int Length
		{
			get
			{
				return End - Start + 1;
			}
		}
		#endregion

		#region Operators
		/// <summary>
		/// Compares two Ascend.FrameInterval structures for equality.
		/// </summary>
		/// <param name="value1">
		/// The first value to compare.
		/// </param>
		/// <param name="value2">
		/// The second value to compare.
		/// </param>
		/// <returns>
		/// Returns true if the Start and End frames of <paramref name="value1"/> are equal to those
		/// of <paramref name="value2"/>. Returns false otherwise.
		/// </returns>
		public static bool operator ==(FrameInterval value1, FrameInterval value2)
		{
			return value1.Equals(value2);
		}

		/// <summary>
		/// Compares two Ascend.FrameInterval structures for inequality.
		/// </summary>
		/// <param name="value1">
		/// The first value to compare.
		/// </param>
		/// <param name="value2">
		/// The second value to compare.
		/// </param>
		/// <returns>
		/// Returns true if the Start and End frames of <paramref name="value1"/> are different from
		/// those of <paramref name="value2"/>. Returns false otherwise.
		/// </returns>
		public static bool operator !=(FrameInterval value1, FrameInterval value2)
		{
			return !value1.Equals(value2);
		}
		#endregion

		#region Public Methods
		/// <summary>
		/// Compares two Ascend.FrameInterval structures for equality.
		/// </summary>
		/// <param name="value">
		/// The Ascend.FrameInterval instance to compare to this instance.
		/// </param>
		/// <returns>
		/// Returns true if the instances are equal. Returns false otherwise.
		/// </returns>
		public bool Equals(FrameInterval value)
		{
			return Start == value.Start && End == value.End;
		}

		/// <summary>
		/// Determines whether the specified object is an Ascend.FrameInterval structure and if so,
		/// whether the Start and End properties of the specified object are equal to those of this
		/// Ascend.FrameInterval instance.
		/// </summary>
		/// <param name="obj">
		/// The object to compare.
		/// </param>
		/// <returns>
		/// Returns true if the specified object is an Ascend.FrameInterval structure and is
		/// identical to this instance. Returns false otherwise.
		/// </returns>
		public override bool Equals(object obj)
		{
			if (obj == null || GetType() != obj.GetType())
			{
				return false;
			}

			return Equals((FrameInterval)obj);
		}

		/// <summary>
		/// Returns a hash code for this Ascend.FrameInterval structure.
		/// </summary>
		/// <returns>
		/// Returns a hash code for this Ascend.FrameInterval structure.
		/// </returns>
		public override int GetHashCode()
		{
			return Start.GetHashCode() ^ End.GetHashCode();
		}

		/// <summary>
		/// Creates a string representation of this Ascend.FrameInterval structure.
		/// </summary>
		/// <returns>
		/// Returns a string containing the Start and End values of this Ascend.FrameInterval
		/// structure.
		/// </returns>
		public override string ToString()
		{
			return Start + " : " + End;
		}
		#endregion

		#region Static Methods
		/// <summary>
		/// Compares two Ascend.FrameInterval structures for equality.
		/// </summary>
		/// <param name="value1">
		/// The first value to compare.
		/// </param>
		/// <param name="value2">
		/// The second value to compare.
		/// </param>
		/// <returns>
		/// Returns true if the Start and End frames of <paramref name="value1"/> are equal to those
		/// of <paramref name="value2"/>. Returns false otherwise.
		/// </returns>
		public static bool Equals(FrameInterval value1, FrameInterval value2)
		{
			return value1.Equals(value2);
		}
		#endregion
	}
}
